package com.imay.live.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.imay.base.adapter.RecyclingPagerAdapter;
import com.imay.live.R;

/**
 * Created by jeaman on 2017/3/20.
 */

public class ChangeRoomShortAdapter extends RecyclingPagerAdapter {
    private Context mContext;
    private int mCount;
    private int mCountOffset;
    private SparseArray<ViewHolder> mViewList = new SparseArray<>();

    public ChangeRoomShortAdapter(Context context) {
        this(context, true);
    }

    public ChangeRoomShortAdapter(Context context, boolean plusOne) {
        mContext = context;
        mCountOffset = plusOne ? 1 : 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        ViewHolder viewHolder;
        if (convertView == null) {
            FrameLayout rootView = new FrameLayout(mContext);
            FrameLayout videoParentView = new FrameLayout(mContext);
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(R.mipmap.pic_03chaodanbo_bg);
            videoParentView.addView(imageView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            rootView.addView(videoParentView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            viewHolder = new ViewHolder();
            viewHolder.rootView = rootView;
            viewHolder.videoParentView = videoParentView;
            viewHolder.coverIv = imageView;
            convertView = rootView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        mViewList.put(position, viewHolder);
        viewHolder.coverIv.setVisibility(View.VISIBLE);
        return convertView;
    }

    @Override
    public int getCount() {
        return mCount == 0 ? 1 : mCount + mCountOffset; //至少返回1条数据
    }

    public void setCount(int count) {
        mCount = count;
        notifyDataSetChanged();
    }

    public ViewGroup getRootView(int position) {
        return mViewList.get(position).rootView;
    }

    public ViewGroup getVideoParentView(int position) {
        return mViewList.get(position).videoParentView;
    }

    private class ViewHolder {
        private ViewGroup rootView;
        private ViewGroup videoParentView;
        private ImageView coverIv;
    }
}
